package com.scs.application.modules.sys.sync.impl;

import com.scs.application.core.utils.BeanUtils;
import com.scs.application.modules.sys.adapter.SerialRuleAdapter;
import com.scs.application.modules.sys.entity.SerialRule;
import com.scs.application.modules.sys.properties.SystemProperties;
import com.scs.application.modules.sys.service.SerialRuleService;
import com.scs.application.modules.sys.sync.SerialRuleSyncService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:编号规则同步实现类
 */
@Service
public class SerialRuleSyncServiceImpl implements SerialRuleSyncService {

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private SerialRuleAdapter serialRuleAdapter;

    /**
     * 同步编号规则
     */
    public void syncSerialRule() {
        SerialRuleService fileSerialRuleService = serialRuleAdapter.getSerialRuleService(false);
        List<SerialRule> fileSerialRules = fileSerialRuleService.list();
        if (fileSerialRules == null || fileSerialRules.isEmpty()) {
            return;
        }

        List<SerialRule> needUpdateSerialRules = Lists.newArrayList();
        List<SerialRule> flows = serialRuleAdapter.getSerialRuleService().list();
        for (SerialRule serialRule : fileSerialRules) {
            if (!flows.stream().filter(flow -> serialRule.getCode().equalsIgnoreCase(flow.getCode())).findAny().isPresent()) {
                SerialRule flow = new SerialRule();
                BeanUtils.copyProperties(serialRule, flow);
                needUpdateSerialRules.add(flow);
            }
        }
        if (needUpdateSerialRules.size() > 0) {
            serialRuleAdapter.getSerialRuleService().saveOrUpdateBatch(needUpdateSerialRules);
        }
    }
}
